<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :okAuth="'sports:sportsWeight:edit'"
    title="附加分"
    width="90%"
  >
    <BasicTable @register="registerTable" @success="handleSuccess">
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsWeight:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
    </BasicTable>
  </BasicDrawer>
  <AdditionalModal @register="registerModal" @success="handleSuccess" />
</template>

<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightAdditional',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import {
    SportsIndexBonusRules,
    sportsIndexBonusRulesListData,
    sportsIndexBonusRulesDelete,
  } from '/@/api/sports/sportsIndexBonusRules';
  import AdditionalModal from './AdditionalModal.vue';
  const { showMessage } = useMessage();
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<SportsIndexBonusRules>({} as SportsIndexBonusRules);
  const tableColumns: BasicColumn[] = [
    {
      title: t('指标名称'),
      dataIndex: 'sportsWeight.indexName',
      key: 'a.index_type',
      //sorter: true,
      align: 'left',
      width: 150,
      dictType: 's_index_type',
      slots: { customRender: 'firstColumn' },
    },
    {
      title: t('适配年级'),
      dataIndex: 'adaptGrade',
      key: 'a.adaptGrade',
      //sorter: true,
      align: 'left',
       width: 330,
      dictType: 'vke_grade',
    },
    {
      title: t('适配性别'),
      dataIndex: 'adaptSex',
      key: 'a.adapt_sex',
      //sorter: true,
      align: 'left',
       width: 80,
      dictType: 'sys_user_sex',
    },
    {
      title: t('测评值'),
      dataIndex: 'score',
      key: 'a.score',
      //sorter: true,
      align: 'left',
      width: 80,
    },
    {
      title: t('附加分'),
      dataIndex: 'bonusPoints',
      key: 'a.bonusPoints',
      //sorter: true,
      align: 'center',
      width: 80,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑附加分'),
        onClick: handleForm.bind(this, { bonusRulesId: record.bonusRulesId }),
        auth: 'sports:sportsClassTeacerRelation:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除'),
        popConfirm: {
          title: t('是否确认删除'),
          confirm: handleDelete.bind(this, { bonusRulesId: record.bonusRulesId }),
        },
        auth: 'sports:sportsClassTeacerRelation:edit',
      },
    ],
  };
  let dataTable;
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner((data) => {
    console.log(data);
    dataTable = data;
    reload();
  });
  const [registerTable, { reload }] = useTable({
    api: sportsIndexBonusRulesListData,
    beforeFetch: (params) => {
      params.indexId = dataTable.indexId;
      console.log(params);
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    canResize: true,
  });
  const [registerModal, { openModal }] = useModal();

  function handleForm(record: Recordable = dataTable) {
    record.indexId = dataTable.indexId;
    record.indexName = dataTable.indexName;
    openModal(true, record);
  }
  async function handleDelete(record: Recordable) {
    const res = await sportsIndexBonusRulesDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
